package com.doradance.main.adapter;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.model.remote.bean.HotListBean;
import com.doradance.util.CollectionUtils;
import com.doradance.view.recyclerview.ViewHolderModel;
import com.yong.android.framework.view.CircleImageView;
import com.yong.android.framework.view.recyclerview.RecyclerViewCell;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/12/28.
 */

public class ListCell extends RecyclerViewCell<ListCell.ItemHolder> {
    private List<HotListBean.ItemBean> list;

    @Override
    public ItemHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        return new ItemHolder(inflater, parent);
    }

    @Override
    protected void onBindViewHolder(ItemHolder holder, final int position) {
        holder.bindModel(list.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(position);
                }
            }
        });
    }

    public void updateSource(List<HotListBean.ItemBean> source) {
        list = source;
    }

    public void updateMore(List<HotListBean.ItemBean> moreSource) {
        if (CollectionUtils.listSize(moreSource) != 0) {
            list.addAll(moreSource);
        }
    }

    @Override
    public int getCount() {
        return CollectionUtils.listSize(list);
    }

    static class ItemHolder extends ViewHolderModel<HotListBean.ItemBean> {
        public Context mContext;
        @Bind(R.id.iv_avatar)
        CircleImageView mIvAvatar;
        @Bind(R.id.tv_name)
        TextView mTvName;
        @Bind(R.id.iv_gender)
        ImageView mIvGender;
        @Bind(R.id.tv_age)
        TextView mTvAge;
        @Bind(R.id.tv_home_top1)
        TextView mTvHomeTop1;
        @Bind(R.id.tv_home_top2)
        TextView mTvHomeTop2;
        @Bind(R.id.tv_home_top3)
        TextView mTvHomeTop3;
        @Bind(R.id.llLayout)
        LinearLayout mLlLayout;
        @Bind(R.id.tv_charm)
        TextView mTvCharm;
        @Bind(R.id.iv_cover)
        ImageView mIvCover;
        @Bind(R.id.tv_title)
        TextView mTvTitle;
        @Bind(R.id.tv_price)
        TextView mTvPrice;
        @Bind(R.id.fl_container)
        FrameLayout mFlContainer;
        @Bind(R.id.home_constraint)
        ConstraintLayout mHomeConstraint;

        public ItemHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_home_anchor, parent, false));
            mContext = parent.getContext();

        }

        @Override
        protected void bindView() {
            ButterKnife.bind(this,itemView);
        }

        @Override
        public void bindModel(HotListBean.ItemBean itemBean) {

            mTvAge.setText(itemBean.getAge());
            mTvName.setText(itemBean.getNickname());
            mTvCharm.setText("魅力值："+itemBean.getCharm());
            Glide.with(mContext).load(itemBean.getAvatar()).into(mIvAvatar);
            Glide.with(mContext).load(itemBean.getCover()).into(mIvCover);
        }
    }

    interface OnItemClickListener{
        void onItemClick(int position);
    }
    private OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        mOnItemClickListener =  onItemClickListener;
    }

}
